﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing.Design;
using System.Linq;

using TeamDeployer.TeamFoundation.Build.Workflow.Design;

namespace TeamDeployer.TeamFoundation.Build.Workflow.CustomTypes
{
    
    [Serializable, DisplayName("Database Deployment Specs"), Category("Deployment"), TypeConverter(
        typeof(DatabaseSpecListConverter)), Editor(typeof(DatabaseSpecListEditor), typeof(UITypeEditor))]
     public  class DatabaseSpecList: List<DatabaseSpec>, ICustomTypeDescriptor
    {
          // Methods
        public DatabaseSpecList()
        {
        }

        public DatabaseSpecList(IList<DatabaseSpec> specs)
        : base(specs)
        {
        }

        public DatabaseSpecList(DatabaseSpec spec)
        : base(new DatabaseSpec[] { spec })
        {
        }

        // Properties
        [Browsable(false)]
        public string RootBuildPath { get; set; }

        public AttributeCollection GetAttributes()
        {
            return TypeDescriptor.GetAttributes(this, true);
        }

        public string GetClassName()
        {
            return TypeDescriptor.GetClassName(this, true);
        }

        public string GetComponentName()
        {
            return TypeDescriptor.GetComponentName(this, true);
        }

        public TypeConverter GetConverter()
        {
            return TypeDescriptor.GetConverter(this, true);
        }

        public EventDescriptor GetDefaultEvent()
        {
            return TypeDescriptor.GetDefaultEvent(this, true);
        }

        public PropertyDescriptor GetDefaultProperty()
        {
            return TypeDescriptor.GetDefaultProperty(this, true);
        }

        public object GetEditor(Type editorBaseType)
        {
            return TypeDescriptor.GetEditor(this, editorBaseType, true);
        }

        public DatabaseSpec[] GetEnabledDatabaseSpecs()
        {
            return this.BaseQuery().ToArray();
        }

        public EventDescriptorCollection GetEvents()
        {
            return TypeDescriptor.GetEvents(this, true);
        }

        public EventDescriptorCollection GetEvents(Attribute[] attributes)
        {
            return TypeDescriptor.GetEvents(this, attributes, true);
        }

        public PropertyDescriptorCollection GetProperties()
        {
            PropertyDescriptorCollection descriptors = new PropertyDescriptorCollection(null);
            for (int i = 0; i < base.Count; i++)
            {
                descriptors.Add(new DatabaseSpecPropertyDescriptor(this, i));
            }
            return descriptors;
        }

        public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
        {
            return this.GetProperties();
        }

        public object GetPropertyOwner(PropertyDescriptor pd)
        {
            return this;
        }

        private IQueryable<DatabaseSpec> BaseQuery() {
           return this.Where(p=> p.Deploy ).AsQueryable();
        }

        public bool HasEnabledConfiguredPackage(string configuration)
        {
            return this.BaseQuery().Any();
        }
    }
}
